/**
 * @filename:TupianServiceImpl Jan 17, 2024
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.mchange.util.StringObjectMap;
import com.vtuzx.core.bean.dto.VtuzxMap;
import com.vtuzx.core.utils.VtuzxUtil;
import com.wonder.app.domain.Tupian;
import com.wonder.app.mapper.TupianDao;
import com.wonder.app.service.TupianService;
import com.wonder.app.request.TupianRequest;
import com.wonder.common.utils.StringUtils;
import com.wonder.common.utils.MessageUtils;
import com.wonder.common.common.AjaxResult;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(图片信息管理服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class TupianServiceImpl  extends ServiceImpl<TupianDao, Tupian> implements TupianService  {

	@Autowired
	private TupianService tupianService;

	@Autowired
	private TupianDao tupianDao;


	/**
     * 验证图片信息管理信息
     * 
     * @param tupian
     * @param type
     * @return String
     */
    @Override
    public String validate(Tupian tupian,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:图片信息管理分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Tupian>   //分页记录
	 */
	@Override 
	public IPage<Tupian> selectList(TupianRequest request, Page<Tupian> page){
	    LambdaQueryWrapper<Tupian> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getFid()) {
	       lambdaQueryWrapper.eq(Tupian::getFid, request.getFid());
	      }               
             
        
		lambdaQueryWrapper.orderByDesc(Tupian::getFid);	
		Page<Tupian> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }


	//提交
	@Override
	public boolean tupianTiJiao(Long fid) {
		Tupian tupian = tupianDao.selectById(fid);

		//获取提交和审核状态
		boolean tj = tupian.getTijiao();
		boolean sh = tupian.getShenhe();
		boolean res ;

		if(tj == true ){
			res = false;
		}else{
			tupian.setTijiao(true);
			res = tupianService.updateById(tupian);
		}
		return res;
	}

	//撤销
	@Override
	public boolean tupianCheXiao(Long fid) {
		Tupian tupian = tupianDao.selectById(fid);

		//获取提交和审核状态
		boolean tj = tupian.getTijiao();
		boolean sh = tupian.getShenhe();
		boolean res ;
		if(tj == true && sh == false){
			tupian.setTijiao(false);
			res = tupianService.updateById(tupian);
			//true;
		}else{
			res = false ;
		}

		return res;
	}

	//审核
	@Override
	public boolean tupianShenHe(Long fid) {
		Tupian tupian = tupianDao.selectById(fid);
		//获取提交和审核的状态
		boolean tj = tupian.getTijiao();
		boolean sh = tupian.getShenhe();
		boolean res ;
		Date currentDate = new Date();

		//当提交和审核状态是 true的时候，停止审核操作
		if(tj == true && sh == true  ){
			res = false;
		}
		else if(tj == true && sh == false ){
			tupian.setShenhe(true);
			tupian.setAuditdate(currentDate);
			res = tupianService.updateById(tupian);
		}
		else{
			res = false;
		}
		return res;
	}

	//反审核
	@Override
	public boolean tupianFanShenHe(Long fid) {
		Tupian tupian = tupianDao.selectById(fid);
		//获取提交和审核的状态
		boolean tj = tupian.getTijiao();
		boolean sh = tupian.getShenhe();
		boolean res ;

		if(tj == true && sh == true ){
			tupian.setTijiao(false);
			tupian.setShenhe(false);
			tupianService.updateById(tupian);
			res = true;
		}else {
			res = false;
		}
		return res;
	}

	@Override
	public List<Map<String, Object>> selectTuPianList() {
		List<Tupian> tupians = tupianDao.selectList(null);
		List<Map<String, Object>> dataList = new ArrayList<>();
		for (Tupian tupian : tupians){
			String tpprocedure_table = tupian.getTpprocedure_table();
			if (null != tpprocedure_table  && !tpprocedure_table.equals("[]")){
				JSONArray jsonArray = new JSONArray(tpprocedure_table);
				Map<String,Object> map = (Map<String, Object>) jsonArray.get(0);
				String name = VtuzxUtil.getString(map,"name");
				String url = VtuzxUtil.getString(map,"url");
				Long fid = tupian.getFid();
				Map<String,Object> data = new HashMap<>();
				data.put("fid",fid);
				data.put("name",name);
				data.put("url",url);
				dataList.add(data);
			}
		}
		return dataList;
	}


}